/*****************************************************************************\

File:   otkRasterExport.java

Date          Build     Author        Version   Comments
28-Aug-12    P-20-13    asonar         $$1       Created
12-Oct-12    P-20-14    asonar         $$2       Updated
19-Oct-12    P-20-15    asonar         $$3       Updated for qcr
22-Jan-15    P-20-64    mrukshad       $$4       Updated
20-mar-15 P-30-05 gshmelev $$5  called GetCurrentSession

\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import java.io.*;

import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSession.*;

/*====================================================================*\
  FUNCTION : otkRasterExport
  PURPOSE  : This examples demonstrates export to different raster 
  file types: bmp, tiff, jpg and eps
  \*====================================================================*/

class otkRasterExport extends DefaultUICommandActionListener
{
    public void otkRasterExport()
    {
    }
    
    public void OnCommand()
    {
	addRasterExport();
    }
    
    public void addRasterExport()
    {
    	
      	try
	    {
		FileWriter file = new FileWriter ("raster_image_info.txt");
		BufferedWriter fp_out_raster_info = new BufferedWriter(file); 
	   
		Session session = pfcSession.GetCurrentSession();
		WSession ses = (WSession)session;
		
		Model model = ses.GetCurrentModel();
		
		if (model != null)
		    {
			System.out.println("Model name: " + model.GetInstanceName() );
		    }
		else
		    throw new Exception("Method: otkExportToRaster \nMsg: Current model is NULL");
		
		
		if (model == null ||( model.GetType() != ModelType.MDL_PART && model.GetType() != ModelType.MDL_ASSEMBLY) )
		    throw new Exception("Method: otkExportToRaster \nMsg: Current model is not a solid model");
		
		Window win = ses.GetModelWindow(model);
		
		if (win != null)
		    {
			
			double rasterHeight = 7.5;
			double rasterWidth = 10.0;
			int count = 1;
			
			fp_out_raster_info.write("Raster Height: " + rasterHeight);
			fp_out_raster_info.newLine();	
			
			fp_out_raster_info.write("Raster Width: " + rasterWidth);
			fp_out_raster_info.newLine();
			
			/* Exporting to bmp format */
			BitmapImageExportInstructions bmp_instrs = 
			    pfcWindow.BitmapImageExportInstructions_Create (rasterWidth ,rasterHeight);
			RasterImageExportInstructions raster_instrs = 
			    (RasterImageExportInstructions)bmp_instrs;
			ExportRasterImage (win, raster_instrs,"bmp", fp_out_raster_info, count++);
			
			/* Exporting to tiff format */	
			TIFFImageExportInstructions tiff_instrs = 
			    pfcWindow.TIFFImageExportInstructions_Create (rasterWidth ,rasterHeight);
			raster_instrs = (RasterImageExportInstructions)tiff_instrs;
			ExportRasterImage (win, raster_instrs,"tiff", fp_out_raster_info, count++);
			
			/* Exporting to jpeg format */	
			JPEGImageExportInstructions jpeg_instrs = 
			    pfcWindow.JPEGImageExportInstructions_Create (rasterWidth ,rasterHeight);
			raster_instrs = (RasterImageExportInstructions)jpeg_instrs;
			ExportRasterImage (win, raster_instrs,"jpg", fp_out_raster_info, count++);
			
			/* Exporting to eps format */		
			EPSImageExportInstructions eps_instrs = 
			    pfcWindow.EPSImageExportInstructions_Create (rasterWidth ,rasterHeight);
			raster_instrs = (RasterImageExportInstructions)eps_instrs;
			ExportRasterImage (win, raster_instrs,"eps", fp_out_raster_info, count++);
			
			fp_out_raster_info.close();
			
		    }
		else
		    {
			throw new Exception("Method: otkExportToRaster \nMsg: Model is not displayed");
		    }
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }		
	
    }
    
    void ExportRasterImage(Window window, RasterImageExportInstructions rasterExportInstrs,
			   String rasterType, BufferedWriter fp_out_raster_info, int count)
    {
	try
	    {
		DotsPerInch dpi = DotsPerInch.RASTERDPI_400;
		RasterDepth depth = RasterDepth.RASTERDEPTH_24;
		
		/*Setting export instructions options */
		rasterExportInstrs.SetDotsPerInch(dpi);
		rasterExportInstrs.SetImageDepth(depth);
		
		/* Getting the model name*/
		Model model = window.GetModel();
		String name = model.GetInstanceName();
		String Lname = name.toLowerCase();
		String rasterFileName = Lname + "." + rasterType;
		
		fp_out_raster_info.write("The type of image " + count + " is: " + rasterType);
		fp_out_raster_info.newLine();	
		
		fp_out_raster_info.write("The file name is: " + rasterFileName);
		fp_out_raster_info.newLine();
		
		/* Saving the model as raster file typein the form: <model name>.<raster file type> */
		
		/* Exporting to raster type*/
		window.ExportRasterImage(rasterFileName, rasterExportInstrs);
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }
	
    }	
    
}
